package vn.hanelsoft.openwifi.model.adapter;

import java.util.ArrayList;

import vn.hanelsoft.openwifi.R;
import vn.hanelsoft.openwifi.common.ConstCommon;
import vn.hanelsoft.openwifi.common.UtilsCommon;
import vn.hanelsoft.openwifi.control.ChromeBrowserActivity;
import vn.hanelsoft.openwifi.control.FreeWifiActivity;
import vn.hanelsoft.openwifi.model.delegate.ServerDelegate;
import vn.hanelsoft.openwifi.model.object.ObjectWifi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

public class AdapterWifi extends BaseAdapter {

	ArrayList<ObjectWifi> listWifi;
	Context mContext;
	boolean checkIsConnected;

	public AdapterWifi(Context mContext, ArrayList<ObjectWifi> listWifi) {
		this.mContext = mContext;
		this.listWifi = listWifi;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return listWifi.size();
	}

	@Override
	public Object getItem(int position) {
		return listWifi.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = convertView;
		final Holder holder;
		if (view == null) {
			LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
			view = inflater.inflate(R.layout.item_wifi, parent, false);

			holder = new Holder();
			holder.nameWifi = (TextView) view.findViewById(R.id.nameWifi);
			holder.nameWifi.setTypeface(UtilsCommon.getFontCustom(mContext));
			holder.lnItemWifi = (LinearLayout) view
					.findViewById(R.id.lnItemWifi);
			holder.progressBar = (ProgressBar) view
					.findViewById(R.id.progressBar);
//			holder.statusWifi = (TextView) view.findViewById(R.id.statusWifi);
//			holder.statusWifi.setTypeface(UtilsCommon.getFontCustom(mContext));
//			holder.statusWifi.setVisibility(View.GONE);
//			holder.typeWifi =  (TextView) view.findViewById(R.id.typeWifi);
//			holder.typeWifi.setTypeface(UtilsCommon.getFontCustom(mContext));
//			holder.typeWifi.setVisibility(View.GONE);
			view.setTag(holder);
		} else {
			holder = (Holder) view.getTag();
		}
		
		if(position == 0)
		{
			holder.lnItemWifi.setBackgroundResource(R.drawable.draw_bg_item_opacity);
		}else if(position %2 == 0)
		{
			holder.lnItemWifi.setBackgroundResource(R.drawable.draw_bg_item_opacity);
		}else{
			holder.lnItemWifi.setBackgroundResource(R.drawable.draw_bg_item2_opacity);
		}

		final ObjectWifi mObjectWifi = (ObjectWifi) getItem(position);
		holder.nameWifi.setText(mObjectWifi.getWifiSSID());
//		holder.typeWifi.setText(UtilsCommon.getStringTypeWifi(mObjectWifi.getTypeWifi()));
		if (UtilsCommon.IsWiFiConnected(mContext)) {
			WifiManager wifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
			WifiInfo wifiInfo = wifiManager.getConnectionInfo();
			if(wifiInfo.getBSSID().equals(mObjectWifi.getWifiBSSID()))
			{
//				holder.statusWifi.setVisibility(View.VISIBLE);
//				holder.statusWifi.setText(mContext
//						.getResources().getString(
//								R.string.connected));
				checkIsConnected = true;
			}else{
				checkIsConnected = false;
			}
		}else
		{
			checkIsConnected = false;
		}

		holder.lnItemWifi.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				UtilsCommon.setStringSetting(mContext, ConstCommon.KEY_WIFI_STORE, "");
				if(checkIsConnected)
				{
					UtilsCommon.setStringSetting(mContext, ConstCommon.KEY_WIFI_STORE, mObjectWifi.getWifiBSSID());
					ServerDelegate.updateTotalUserWifi(null, mObjectWifi.getWifiBSSID(), "1");
					FreeWifiActivity.checkDisconnectWifi = false;
					Intent intent = new Intent(mContext, ChromeBrowserActivity.class);
					((Activity) mContext).startActivity(intent);
					ConstCommon.isServiceRunning = true;
				}else{
					holder.progressBar.setVisibility(View.VISIBLE);
//					holder.statusWifi.setText(mContext.getResources().getString(
//							R.string.connecting));
//					holder.statusWifi.setVisibility(View.VISIBLE);
					UtilsCommon.setStringSetting(mContext,
							ConstCommon.KEY_WIFI_SSID, "");
					connectWifiInBackground(holder, mObjectWifi);
				}
			}
		});

		return view;
	}

	class Holder {
		TextView nameWifi;//, statusWifi, typeWifi;
		LinearLayout lnItemWifi;
		ProgressBar progressBar;
	}

	void connectWifiInBackground(final Holder holder,
			final ObjectWifi mObjectWifi) {
		new Thread(new Runnable() {
			@Override
			public void run() {
				UtilsCommon.connectWifi(mContext, mObjectWifi.getWifiSSID(),
						mObjectWifi.getWifiPassword());
				try {
					Thread.sleep(3000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if (UtilsCommon.IsWiFiConnected(mContext)) {
					UtilsCommon.setStringSetting(mContext,
							ConstCommon.KEY_WIFI_SSID,
							mObjectWifi.getWifiSSID());
					
					UtilsCommon.setStringSetting(mContext, ConstCommon.KEY_WIFI_STORE, mObjectWifi.getWifiBSSID());
					((Activity) mContext).runOnUiThread(new Runnable() {
						@Override
						public void run() {
							if (holder.progressBar.getVisibility() == View.VISIBLE) {
								holder.progressBar.setVisibility(View.GONE);
//								holder.statusWifi.setText(mContext
//										.getResources().getString(
//												R.string.connected));
								
								ServerDelegate.updateTotalUserWifi(null, mObjectWifi.getWifiBSSID(), "1");
								
								FreeWifiActivity.checkDisconnectWifi = false;
								Intent intent = new Intent(mContext, ChromeBrowserActivity.class);
								((Activity) mContext).startActivity(intent);
								((Activity) mContext).overridePendingTransition(
										R.drawable.activity_transaction_trans_left_in,
										R.drawable.activity_transaction_trans_left_out);
							}
						}
					});
				} else {
					try {
						Thread.sleep(5000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					if (UtilsCommon.IsWiFiConnected(mContext)) {
						UtilsCommon.setStringSetting(mContext,
								ConstCommon.KEY_WIFI_SSID,
								mObjectWifi.getWifiSSID());
						
						UtilsCommon.setStringSetting(mContext, ConstCommon.KEY_WIFI_STORE, mObjectWifi.getWifiBSSID());
						((Activity) mContext).runOnUiThread(new Runnable() {
							@Override
							public void run() {
								if (holder.progressBar.getVisibility() == View.VISIBLE) {
									holder.progressBar.setVisibility(View.GONE);
//									holder.statusWifi.setText(mContext
//											.getResources().getString(
//													R.string.connected));
									
									ServerDelegate.updateTotalUserWifi(null, mObjectWifi.getWifiBSSID(), "1");
									
									FreeWifiActivity.checkDisconnectWifi = false;
									Intent intent = new Intent(mContext, ChromeBrowserActivity.class);
									((Activity) mContext).startActivity(intent);
									((Activity) mContext).overridePendingTransition(
											R.drawable.activity_transaction_trans_left_in,
											R.drawable.activity_transaction_trans_left_out);
								}
							}
						});
					} else {
						UtilsCommon.disConnectWifi(mContext);
						((Activity) mContext).runOnUiThread(new Runnable() {
							@Override
							public void run() {
								if (holder.progressBar.getVisibility() == View.VISIBLE) {
									holder.progressBar.setVisibility(View.GONE);
//									holder.statusWifi.setText(mContext
//											.getResources().getString(
//													R.string.connectfailt));
								}
							}
						});
					}
				}
			}
		}).start();
	}
}
